package i18n;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * ResourceBundle基本使用
 * 
 */
public class ResourceBundleTest {
	public static void main(String[] args) {
//		test1();
//		test2();
//		getLocale();
		test3();
	}

	private static void test3() {
		System.out.println(Locale.CHINA.getDisplayName());
		ResourceBundle rb = ResourceBundle.getBundle("i18n/res",Locale.CHINA);
		Enumeration<String> keys=rb.getKeys();
		while(keys.hasMoreElements()){
			String key=keys.nextElement();
			String value=rb.getString(key);
			System.out.println(key+","+value);
		}
	}

	/**
	 * 获得Locale的几种方式
	 */
	private static void getLocale() {
		Locale l1 = Locale.getDefault();
		Locale l2 = Locale.CHINA;
		Locale l3 = new Locale("en", "US");
	}

	// 资源文件命名：baseName_语言代码＿国家代码.properties
	// ResourceBundle使用如下
	// ResourceBundle rb = ResourceBundle.getBundle("baseName",locale);
	// ResourceBundle依据locale提供的信息查找对应的资源文件
	private static void test2() {
		ResourceBundle rb = ResourceBundle.getBundle("i18n/res", Locale
				.getDefault());
		System.out.println(Locale.getDefault().getCountry() + ","
				+ Locale.getDefault().getLanguage());
		System.out.println(rb.getString("title"));
		rb = ResourceBundle.getBundle("i18n/res", Locale.UK);
	}

	private static void test1() {
		// classpath,使用的classLoader,也可以使用i18n.res1，底层把点换为斜杠，会自动加上.properties
		ResourceBundle rb = ResourceBundle.getBundle("i18n/zh");
		String value = rb.getString("title");
		System.out.println(value);
	}
}
